import { SELECT_PHOTO} from  '../actions/actions'
import  Data from '../constant/data'
const data = Data()
const  initialState = {
    //filter的用法是判断每个元素中的属性是否和要比较的属性相等，
    // 相等则过滤掉 不相等就保留 然后返回过滤以后的数组
    images:data.filter((image) => image.num != 1),
    activeImage:data[0],
    isChange: true,
    clickcount: 0
}
export default function(state = initialState,  action) {
        switch (action.type) {
            case SELECT_PHOTO:
                let activeImage = action.oldImage,
                    isChange = action.newImageNum != activeImage.num,
                    images = action.images
                //console.log(action.newImageNum)
                //console.log(activeImage.num)
                if(isChange){
                    let len = images.length
                    //把中间的图片放进数组中
                    images[len] = activeImage
                    for(let i =0;i < len ; i++){
                        if(action.newImageNum == images[i].num){
                            activeImage = images.splice(i, 1)[0]
                        }
                    }
                }
                return {
                    images:images,
                    activeImage:activeImage,
                    isChange: isChange,
                }

        }
    return state
}